﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Scatter
{
    public class AutoClickManager
    {
        private static MainWindow mainWindow;

        private static Thread worker;
        private static Thread productChecker;
        private static Thread datePicker;

        public static void StartAutoClick(MainWindow mainWindow, ConfirmMode confirmMode)
        {
            AutoClickManager.mainWindow = mainWindow;

            AutoClick autoClick = new AutoClick(confirmMode, false);
            worker = new Thread(autoClick.DoWork);
            worker.Start();

            if (confirmMode == ConfirmMode.SignalMode)
            {
                autoClick = new AutoClick(confirmMode, true);
                productChecker = new Thread(autoClick.DoCheckProductWork);
                productChecker.Start();

                autoClick = new AutoClick(confirmMode, true);
                datePicker = new Thread(autoClick.DoPickupDateWork);
                datePicker.Start();
            }
        }

        private static object syncObj = new object();
        private static bool gotIt = false;

        public static void GotIt()
        {
            lock (syncObj)
            {
                if (gotIt == false)
                {
                    gotIt = true;
                    mainWindow.Dispatcher.BeginInvoke(new Action(mainWindow.GotIt));
                }
            }
        }

        public static void AbortAllThread()
        {
            if (worker != null)
            {
                worker.Abort();
            }
            if (productChecker != null)
            {
                productChecker.Abort();
            }
            if (datePicker != null)
            {
                datePicker.Abort();
            }
        }

        public static void UpdateWorkerStatus(string status)
        {
            mainWindow.Dispatcher.BeginInvoke(new Action<string>(mainWindow.UpdateWorkerStatus), status);
        }

        public static void UpdateDatePickerStatus(string status)
        {
            mainWindow.Dispatcher.BeginInvoke(new Action<string>(mainWindow.UpdateDatePickerStatus), status);
        }

        public static void ShowCartItemQuantity(int quantity)
        {
            mainWindow.Dispatcher.BeginInvoke(new Action<int>(mainWindow.ShowCartItemQuantity), quantity);
        }
    }
}
